<include file="public@header_vue"/>
<script src="https://cdn.jsdelivr.net/npm/vue-ueditor-wrap@latest/lib/vue-ueditor-wrap.min.js"></script>
<script type="text/javascript" charset="utf-8" src="__STATIC__/UEditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__STATIC__/UEditor/ueditor.all.min.js"></script>
</head>
<style>
    .el-pagination {
        margin-top: 20px;
    }


    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .photos {
        width: 120px;
        height: 120px;
        border-radius: 6px;
        overflow: hidden;
        float: left;
        margin-right: 10px;
        margin-bottom: 10px;
        position: relative;
    }

    .photos-options {
        position: absolute;
        top: 0;
        left: 0;
        height: 120px;
        line-height: 120px;
        width: 120px;
        text-align: center;
        background-color: rgba(0, 0, 0, 0.6);
        z-index: 10;
        opacity: 0;
        transition: opacity 0.6s;
    }

    .photos:hover .photos-options {
        opacity: 1;
    }

    .photos-options .el-button {
        font-size: 24px;
        color: white
    }

    .photos img {
        width: 120px;
        height: 120px;
    }

    .photos-uploader {
        width: 120px;
        height: 120px;
        float: left;
    }

    .logo-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .logo-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .logo-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 120px;
        height: 120px;
        line-height: 120px;
        text-align: center;
    }
    [v-cloak] {
        display: none;
    }
</style>
<body>
<div id="app" style="height: 800px;">
    <el-tabs type="border-card" v-model="activeCard" @tab-click="handleCard">
        <el-tab-pane label="直播列表" name="live"  v-loading="onDataLoading">

            <el-row>
                <el-button :span="4" type="primary" size="mini" style="float: right;margin-bottom: 10px;" v-if="role_id == 3" @click="edit(null)">新增</el-button>
                <el-col :span="24" v-if="role_id != 3">
                    <el-form :inline="true" :model="form" class="demo-form-inline" size="mini" v-cloak>

                        <el-form-item label="主题：">
                            <el-input v-model="form.theme" placeholder="请输入直播主题"></el-input>
                        </el-form-item>

                        <el-form-item label="公司电话：">
                            <el-input v-model="form.contact_mobile" placeholder="请输入公司电话"></el-input>
                        </el-form-item>

                        <el-form-item label="删除：" v-cloak>
                            <el-select v-model="form.is_delete" placeholder="请选择">
                                <el-option v-for="(item,index) in is_delete" :label="item" :value="index"></el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="发布时间：">
                            <el-date-picker v-model="form.date" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="timestamp" style="width: 220px"></el-date-picker>
                        </el-form-item>

                        <el-form-item>
                            <el-button type="danger" @click="clearForm()" icon="el-icon-refresh">清空</el-button>
                            <el-button type="primary" @click="getData()" icon="el-icon-search">查询</el-button>
                        </el-form-item>

                    </el-form>
                </el-col>
            </el-row>

            <el-table :data="tableData" style="width: 100%" v-cloak>
                <el-table-column prop="id" label="编号"></el-table-column>

                <el-table-column prop="theme" label="直播主题" show-overflow-tooltip min-width="150"></el-table-column>

                <el-table-column prop="start_time" label="开始时间" show-overflow-tooltip min-width="170">
                    <template slot-scope="scope" v-cloak>
                        <span>{{formatDateAll(scope.row.start_time)}}</span>
                    </template>
                </el-table-column>
<!--                <el-table-column prop="end_time"   label="结束时间" show-overflow-tooltip min-width="170">-->
<!--                    <template slot-scope="scope" v-cloak>-->
<!--                        <span>{{formatDateAll(scope.row.end_time)}}</span>-->
<!--                    </template>-->
<!--                </el-table-column>-->

                <el-table-column prop="online_number" label="虚拟人数" min-width="120"></el-table-column>

                <el-table-column prop="name" label="公司名称" show-overflow-tooltip min-width="150">
                    <template slot-scope="scope" v-cloak>
                        <span>{{scope.row.company.name}}</span>
                    </template>
                </el-table-column>

                <el-table-column prop="contact_mobile" label="公司电话" show-overflow-tooltip width="150">
                    <template slot-scope="scope" v-cloak>
                        <span>{{scope.row.company.contact_mobile}}</span>
                    </template>
                </el-table-column>

                <el-table-column prop="user_login" label="直播账号" show-overflow-tooltip width="150">
                    <template slot-scope="scope" v-cloak>
                        <span>{{scope.row.live_account.user_login}}</span>
                    </template>
                </el-table-column>

                <el-table-column prop="is_delete" label="是否删除" show-overflow-tooltip width="150">
                    <template slot-scope="scope" v-cloak>
                        <span v-if="scope.row.is_delete == 1" style="color:red;">已删除</span>
                        <span v-else style="color:green;">未删除</span>
                    </template>
                </el-table-column>


                <el-table-column prop="is_open" label="状态" show-overflow-tooltip width="150">
                    <template slot-scope="scope" v-cloak>
                        <span v-if="scope.row.is_open == 0">未开始</span>
                        <span v-else-if="scope.row.is_open == 1">直播中</span>
                        <span v-else-if="scope.row.is_open == 2">已关闭</span>
                        <span v-else>离开</span>
                    </template>
                </el-table-column>

                <el-table-column prop="create_time" label="发布时间" show-overflow-tooltip min-width="170">
                    <template slot-scope="scope" v-cloak>
                        {{formatDateAll(scope.row.create_time)}}
                    </template>
                </el-table-column>


                <el-table-column label="操作" fixed="right" min-width="200">
                    <template slot-scope="scope">
                        <!--<el-button type="text" v-if="scope.row.is_shenhe == 0" @click="shenhe(scope.row.id)">审核</el-button>-->
                        <!--<el-button type="text" v-if="scope.row.is_shenhe == 0" @click="jujue(scope.row)">拒绝</el-button>-->
                        <el-button type="text" @click="edit(scope.row)" v-if="role_id == 3">编辑</el-button>
                        <el-button type="text" @click="edit(scope.row)" v-else>查看</el-button>
                    </template>
                </el-table-column>

            </el-table>
            <el-pagination background layout="prev, pager, next" :page-size="15" :total="totalNum" :current-page.sync="currentPage" @current-change="getData"></el-pagination>

        </el-tab-pane>

        <el-dialog :visible.sync="dialogVisible" v-loading.fullscreen="layerloading">

            <el-form :model="editForm" :rules="rules" ref="editForm" label-width="100px" class="demo-ruleForm">

<!--                <el-form-item label="账号" prop="account_id">-->
<!--                    <el-select v-model="editForm.account_id" placeholder="请选择账号">-->
<!--                        <el-option v-for="(item,index) in account" :label="item.user_login" :value="item.id"></el-option>-->
<!--                    </el-select>-->
<!--                </el-form-item>-->

                <el-form-item label="直播主题" prop="theme">
                    <el-col :span="15">
                        <el-input v-model="editForm.theme" placeholder="请输入直播主题"></el-input>
                    </el-col>
                </el-form-item>

                <el-form-item label="开始时间" required>
                    <el-col :span="11">
                        <el-form-item prop="date1">
                            <el-date-picker type="date" value-format="timestamp" placeholder="选择日期" v-model="editForm.date1" v-bind:disabled="role_id == 3 && editForm.id !== '' && timestamp >= editForm.date2" style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="1"><div style="text-align: center;">-</div></el-col>
                    <el-col :span="11">
                        <el-form-item prop="date2">
                            <el-time-picker value-format="timestamp" placeholder="选择时间" v-model="editForm.date2" v-bind:disabled="role_id == 3 && editForm.id !== '' && timestamp >= editForm.date2" style="width: 100%;"></el-time-picker>
                        </el-form-item>
                    </el-col>
                </el-form-item>

<!--                <el-form-item label="结束时间" required>-->
<!--                    <el-col :span="11">-->
<!--                        <el-form-item prop="date3">-->
<!--                            <el-date-picker type="date" value-format="timestamp" placeholder="选择日期" v-model="editForm.date3" style="width: 100%;"></el-date-picker>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
<!--                    <el-col :span="1"><div style="text-align: center;">-</div></el-col>-->
<!--                    <el-col :span="11">-->
<!--                        <el-form-item prop="date4">-->
<!--                            <el-time-picker value-format="timestamp" placeholder="选择时间" v-model="editForm.date4" style="width: 100%;"></el-time-picker>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
<!--                </el-form-item>-->


                <el-form-item label="预告图" prop="trailer_img">
                    <div v-for="photo in editForm.trailer_img" class="photos">
                        <img :src="photo"/>
                        <div class="photos-options">
                            <el-button icon="el-icon-delete" type="text" @click="handleRemove(photo)"></el-button>
                        </div>
                    </div>
                    <div v-if="editForm.trailer_img !== undefined && editForm.trailer_img.length == 0">
                        <el-upload class="photos-uploader logo-uploader" action="/admin/Live/goodsImgUploader" :headers="headers" :show-file-list="false" :on-success="handleintroimgSuccess" multiple>
                            <i class="el-icon-plus logo-uploader-icon"></i>
                        </el-upload>
                        <div class="el-form-item__error" style="margin-left: 100px;">
                            图片宽高分辨率 1080*1920px 以上
                        </div>
                    </div>

                </el-form-item>

                <el-form-item label="虚拟人数" prop="online_number" style="margin-top: 15px;">
                    <el-col :span="12">
                        <el-input v-model="editForm.online_number" placeholder="请输入虚拟人数"  oninput="value=value.replace(/[^0-9]/g,'')"></el-input>
                    </el-col>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button v-if="editForm.id === ''" type="primary" @click="submit('editForm')">新增</el-button>
            <el-button v-if="role_id == 3 && editForm.id !== ''" type="primary" @click="submit('editForm')">修改</el-button>
            </span>
        </el-dialog>



    </el-tabs>
</div>
</body>
<script>
    Vue.component('vue-ueditor-wrap', VueUeditorWrap)
    var app = new Vue({
        el: '#app',
        data: function () {

            var checkIdCard = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('身份证号码不能为空！'));
                } else {
                    // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
                    const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
                    console.log(reg.test(value));
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error('请输入正确的身份证号码！'));
                    }
                }
            };
            var checkPhone  = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('手机号不能为空'));
                } else {
                    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                    console.log(reg.test(value));
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error('请输入正确的手机号'));
                    }
                }
            };

            return {
                role_id:null,
                account:[],
                activeCard:'live',

                tableData: [],

                is_delete:['未删除','已删除'],
                onDataLoading: false,
                totalNum: 0,
                currentPage: 1,


                headers: {'X-Requested-With': 'XMLHttpRequest'},
                activeName: 'price',
                form: {
                    theme:'',
                    contact_mobile: '',
                    is_delete:'',
                    date:''
                },

                dialogVisible:false,
                layerloading:false,
                editForm: {
                    id:'',
                    theme:'', //主题
                    company_id:'',  //公司id
                    account_id:'',  //账号id
                    user_login:'',
                    trailer_img:[], //直播预告图
                    date1:'',
                    date2:'',
                    date3:'',
                    date4:'',
                    start_time:'',  //直播的开始时间
                    end_time:'',    //直播的结束时间
                    minutes:'',     //总计分钟
                    create_time:'', //新增时间
                    is_delete:'',   //是否删除；0未删除；1是已删除
                    pushurl:'',     //推流地址
                    rtmpurl:'',     //播流地址
                    flvurl:'',
                    m3u8url:'',
                    is_overdue:'',  //是否过期；1是过期；0是未过期
                    second:'',      //已经直播的秒数
                    online_number:0,//虚拟人数
                },

                rules: {
                    account_id: [{required: true, message: '请选择账号', trigger: 'change' }],

                    theme: [{ required: true, message: '请输入直播主题', trigger: 'blur' },{ min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur' }],

                    date1: [
                        { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
                    ],
                    date2: [
                        { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
                    ],

                    // date3: [
                    //     { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
                    // ],
                    // date4: [
                    //     { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
                    // ],

                    trailer_img: [{ required: true, message: '请上传直播预告图', trigger: 'blur' }],

                    online_number: [{ required: true, message: '请输入虚拟人数', trigger: 'blur' }],

                },

                timestamp:'',
            }
        },
        created() {
            this.getData()
        },
        methods: {

            clearForm () {
                console.log(this.form);
                this.form = {
                    theme:'',
                    contact_mobile: '',
                    is_delete:'',
                    date: '',
                }
            },

            handleRemove(url) {
                let index = this.editForm.trailer_img.indexOf(url);
                this.editForm.trailer_img.splice(index, 1);
            },


            handleintroimgSuccess(res, file){
                if (res.code === 1) {
                    this.editForm.trailer_img.push(res.data.url);
                } else {
                    if (res.msg == '非法上传！') {
                        window.location.href = '/admin/public/login';
                        this.$message.error('登录失效！');return false;
                    }

                    this.$message.error(res.msg)
                }
            },

            submit (form) {
                this.dialogLoading = true;

                // if (this.editForm.id !== '') {
                //     let timer = Date.parse(new Date());
                //     this.timestamp = timer;
                // }


                this.$refs[form].validate((valid) => {
                    if (valid) {
                        console.log(this.form);
                        axios.post('/admin/Live/submit', this.editForm, {
                            headers: {
                                'X-Requested-With': 'XMLHttpRequest',
                            }
                        }).then((response) => {
                            this.dialogLoading = false;
                            if (response.data.code === 1) {
                                if (this.editForm.id === '') {
                                    this.$message.success('新增完成');
                                } else {
                                    this.$message.success('修改成功');
                                }
                                this.dialogVisible = false;
                                this.getData(this.currentPage);
                            } else {
                                this.$message.warning(response.data.msg);
                                this.dialogLoading = false;
                                this.getData(this.currentPage);
                            }
                        }).catch((error) => {
                            this.dialogLoading = false;
                            this.$message.error(error.toString())
                        })
                    } else {
                        this.dialogLoading = false;
                        return false;
                    }
                });
            },

            handleCard (tab, event) {
                console.log(event);
            },


            getData(page = 1) {
                let _this = this;
                this.onDataLoading = true;
                let theme     = '';
                let contact_mobile = '';
                let is_delete = '';
                let date   = '';
                if (_this.form.theme !== '') {
                    theme = this.form.theme;
                }
                if (_this.form.contact_mobile !== '') {
                    contact_mobile = this.form.contact_mobile;
                }
                if (_this.form.is_delete !== '') {
                    is_delete = this.form.is_delete;
                }
                if (_this.form.date !== '') {
                    date = this.form.date;
                }
                let params = { page:page , theme:theme , contact_mobile:contact_mobile ,is_delete:is_delete, date:date };
                axios.get('/admin/Live/room_list', {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                    params: params
                }).then(function (response) {
                    console.log(response);
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.tableData = response.data.data.list.data;
                        _this.totalNum  = response.data.data.list.total;
                        _this.role_id   = response.data.data.role_id;
                        if (response.data.data.role_id === 3) {
                            _this.account = response.data.data.account;

                            if (response.data.data.account.length > 0) {
                                console.log('ceshi');
                                console.log(response.data.data.account[0]['id']);
                                _this.editForm.account_id = response.data.data.account[0]['id'];
                                _this.editForm.user_login = response.data.data.account[0]['user_login'];
                                console.log('ceshi2');
                                console.log(_this.editForm.account_id);
                            }
                        }
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },

            edit (row) {

                let timer = Date.parse(new Date());
                this.timestamp = timer;

                if (row == null) {
                    let account_id = this.editForm.account_id;
                    let user_login = this.editForm.user_login;
                    this.editForm.account_id = account_id; //账号id
                    this.editForm.user_login = user_login;

                    this.editForm.id = '';
                    this.editForm.theme = ''; //主题
                    this.editForm.company_id  = '';
                    this.editForm.trailer_img = []; //直播预告图
                    this.editForm.date1 = '';
                    this.editForm.date2 = '';
                    this.editForm.date3 = '';
                    this.editForm.date4 = '';
                    this.editForm.start_time = ''; //直播的开始时间
                    this.editForm.end_time = '';   //直播的结束时间
                    this.editForm.minutes = '';    //总计分钟
                    this.editForm.create_time = '';//新增时间
                    this.editForm.is_delete = '';  //是否删除；0未删除；1是已删除
                    this.editForm.pushurl = '';    //推流地址
                    this.editForm.rtmpurl = '';    //播流地址
                    this.editForm.flvurl = '';
                    this.editForm.m3u8url = '';
                    this.editForm.is_overdue = ''; //是否过期；1是过期；0是未过期
                    this.editForm.second = '';     //已经直播的秒数
                    this.editForm.online_number = 0;//虚拟人数

                } else {
                    console.log(row);
                    console.log('rerer');

                    if (this.role_id !== 3) {
                        if (this.account.length === 0) {
                            this.account.push(row.live_account);
                        }
                    }

                    this.editForm = JSON.parse(JSON.stringify(row));//深拷贝
                }
                console.log(this.editForm);
                this.dialogVisible = true;
            },


            formatDateFull(timestamp) {
                let date = new Date(timestamp * 1000);
                return date.getFullYear() + '-' + this.prefix(date.getMonth() + 1) + '-' + this.prefix(date.getDate());
            },
            formatDateAll(timestamp) {
                let date = new Date(timestamp * 1000);
                return date.getFullYear() + '-' + this.prefix(date.getMonth() + 1) + '-' + this.prefix(date.getDate()) + ' ' + this.prefix(date.getHours()) + ':' + this.prefix(date.getMinutes()) + ':' + this.prefix(date.getSeconds());
            },
            formatDate(timestamp) {
                let date = new Date(timestamp * 1000);
                return date.getFullYear() + '-' + this.prefix(date.getMonth() + 1) + '-' + this.prefix(date.getDate());
            },
            prefix(num) {
                if (num < 10) {
                    return '0' + num;
                }
                return num;
            }
        }
    });
</script>
